<?php

namespace frontend\modules\resource\controllers;

use frontend\modules\resource\logic\BranchResourceLogic;
use frontend\modules\resource\logic\ResourceLogic;
use frontend\modules\resource\logic\SpecialLogic;
use Yii;
use yii\web\NotFoundHttpException;

/**
 * ResourceController implements the CRUD actions for HyBook model.
 */
class BranchResourceController extends ResourceController
{
    /**
     * @throws \yii\base\ExitException
     */
    public function init()
    {
        $this->checkBrId('branchIdForBranchResource');
        $this->checkType('typeForBranchResource', '/resource/branch-resource/index?type=1');

    }

    /**
     * @return string
     * @throws \yii\base\ExitException
     */
    public function actionIndex()
    {
        $searchModel = (new ResourceLogic())->getResourceModelSearch($this->type);

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $this->branchId);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'type' => $this->type,
        ]);
    }

    /**
     * 设置推荐和首页接口
     * @return string
     * @throws \yii\base\ExitException
     * @throws \yii\db\Exception
     */
    public function actionBatchSupport()
    {

        $id = Yii::$app->request->post('id');
        $r_type = Yii::$app->request->post('r_type');//资源类型
        $sup_type = Yii::$app->request->post('type');//1首页 2推荐
        $id = json_decode($id, true);
        $res = (new BranchResourceLogic())->AddSupport($id, $sup_type, $r_type, $this->branchId);
        return $this->suc(['data' => $res]);


    }

    /**
     * 批量移除移除资源
     * @return string
     * @throws \yii\base\ExitException
     * @throws \yii\db\Exception
     */
    public function actionBatchRemove()
    {

        $id = Yii::$app->request->post('id');
        $r_type = Yii::$app->request->post('r_type');//资源类型
        $id = json_decode($id, true);
        $res = (new BranchResourceLogic())->RemoveBranchResource($id,$r_type, $this->branchId);
        return $this->suc(['data' => $res, 'msg' => '移除完成']);
    }


    /**
     * 设置专题接口
     * @return string
     * @throws \yii\base\ExitException
     * @throws \yii\db\Exception
     */
    public function actionBatchSpecial()
    {

        $id = Yii::$app->request->post('id');
        $spId = Yii::$app->request->post('sp_id');
        $r_type = Yii::$app->request->post('r_type');//资源类型
        $id = json_decode($id, true);
        $res = (new BranchResourceLogic())->AddSpecialResource($id, $spId,$r_type, $this->branchId);

        return $this->suc(['data' => $res]);

    }


    /**
     * 专题列接口
     * @return string
     */
    public function actionOption()
    {

        $data = (new SpecialLogic)->specialMap();
        $tmp = '';
        foreach ($data as $key => $val) {
            $tmp .= "<option value='" . $key . "'>{$val}</option>";
        }
        return $this->suc(['data' => $tmp]);
    }
}
